<?php

class Utils {
        public static function extractZip($zipFile, $extractTo){
            
                if(is_file($zipFile)){
                        $zip=new ZipArchive();
                        
                        if($zip->open($zipFile)){
                                $folderName='fol_' . time();
                                
                                $extractTo.=DIRECTORY_SEPARATOR . $folderName;
                            
                                if(!is_dir($extractTo)){
                                        mkdir($extractTo, 0777, true);
                                }
                                
                                $zip->extractTo($extractTo);
                                
                                $zip->close();
                                
                                return array('fullpath'=>$extractTo, 'foldername'=>$folderName);
                        }
                        
                }
                
                return null;
        }
        
        public static function createAbsoluteUrl($link){
                return Yii::app()->params['serverAddress'] . Yii::app()->baseUrl . '/' . $link;
        }
        
        public static function resizeImage($imagePathname, $newImagePathname, $toWidth, $toHeight, $checkNeedResized=false, $resizeType=  Image::AUTO){
                $image=Yii::app()->image->load($imagePathname);
                
                $isResized=true;
                if($checkNeedResized && ($image->width < $toWidth && $image->height < $toHeight))
                        $isResized=false;
                if($isResized){
                        $image->resize($toWidth, $toHeight, $resizeType);

                        $image->save($newImagePathname);
                        
                        return true;
                }else{
                        //image not be resized so return false
                        return false;
                }
        }
}

?>
